package algorithm.ldk.Vaild_Parentheses;

import java.util.Stack;

/**
 * @作者：ldk
 * @email 2505496685@qq.com
 * @描述：默认描述
 * @创建时间：23:48 2021-08-26
 * @修改时间：23:48 2021-08-26
 * @修改描述：默认描述
 */

/**
 * 括号匹配算法
 */
public class Solution {
    public boolean isValid(String s ){

        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[' ){
                stack.push(c);
            }else {
                if (stack.isEmpty()){
                    return false;
                }
                char topChar = stack.pop();
                if (c == ')' && topChar != '('){
                    return false;
                }
                if (c == ']' && topChar != '['){
                    return false;
                }
                if (c == '}' && topChar != '{'){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }


    public static void main(String[] args) {
        System.out.println(new Solution().isValid("[]{}()"));
        System.out.println(new Solution().isValid("([)]"));
    }
}
